//
//  HMHomeCell.swift
//  WB25
//
//  Created by HM on 2017/7/4.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit
class HMHomeCell: UITableViewCell {
    
    // 记录底部视图的顶部约束 便于卸载和赋值
    var bottomViewTopConstraint: Constraint?
    
    // 提供一个模型属性 供外界赋值
    var statusViewModel: HMStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel

            /*
                - 卸载原有约束
                - 判断是否有转发微博
                    - 判断值 retweeted_status
                        - 如果retweeted_status != nil
                            - 有转发微博
                                - 转发微博赋值
                                - bottomView.top = retweetView.bottom
                                - 显示转发微博(cell复用问题)
                        - 如果retweeted_status == nil
                            - 没有转发转发微博
                                - bottomView.top =  originalView.bottom
                                - 隐藏转发微博(cell复用问题)
             */
            
            // 卸载约束
            bottomViewTopConstraint?.deactivate()
            
            // 有值
            if let _ = statusViewModel?.homeModel?.retweeted_status{
                // 赋值
                retweetView.statusViewModel = statusViewModel
                // bottomView.top = retweetView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                // 显示转发微博
                retweetView.isHidden = false
            }else{
                // 没有转发微博
                // ottomView.top =  originalView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
                // 隐藏转发微博
                retweetView.isHidden = true
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        // 添加约束
        originalView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            // 它的这个约束应该是谁决定 -> 自己决定
//            make.height.equalTo(50)
            make.top.equalTo(8)
        }
        
        retweetView.snp.makeConstraints { (make) in
        // 它的这个约束应该是谁决定 -> 自己决定
//            make.height.equalTo(50)
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp.bottom)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
    }
    
    
    // 懒加载控件
    // 原创微博
    lazy var originalView: HMHomeOriginalView = HMHomeOriginalView()
    // 转发微博
    private lazy var retweetView: HMHomeRetweetView = HMHomeRetweetView()
    // 底部视图
    private lazy var bottomView: HMHomeBottomView = HMHomeBottomView()

}
